<?php defined('SYSPATH') or die('No direct script access.');

class Model_ProductParser_FromShop_Wildberries extends Model_ProductParser_Abstract
{   
    protected static $_instance = null;
    
    protected $_product = null;
 
    public function parse(Model_Product $product)
    {
        $this->_product = $product;
        
        $url  = $this->_get_clean_product_url();
        
        $html = $this->_get_source_code($url);
        
        $html = $this->_encode($html);
        
        try {
            DB::insert('product_html', array('product_id', 'html'))
                ->values(array($this->_product->id, $html))
                ->execute();
        } catch (Database_Exception $e) {
            if (strstr($e->getMessage(), 'Duplicate entry') !== false) {
                DB::update('product_html')
                    ->set(array('html' => $html))
                    ->where('product_id', '=', $this->_product->id)
                    ->execute();    
            } else {
                die($e->getMessage());    
            }
            
        }
        
        $color          = $this->_get_color($html);
        $consist        = $this->_get_consist($html);       
        //$color_products = $this->_get_color_products($html);      
        $images         = $this->_get_images($html);        
        $params         = $this->_get_params($html);
        
        if ($color) {
            $params['Цвет'] = $color;   
        }
        if ($consist) {
            $params['Состав'] = $consist;    
        }
        $images = implode('||', $images);
        if (count($params)) {
            $params = json_encode($params);
            $clean_params = Model_Product::get_clean_params($params);    
        } else {
            $params = $clean_params = null;
        }
        
        
        
        /*if ($images) {
            $this->_product->images = $images;   
        }
        
        if (count($params)) {
            //$params = json_encode($params);
            $this->_product->params = $params;   
        }  */
        DB::update('product_data')
                ->set(array(
                        'images' => $images,
                        'params' => $params,
                        'clean_params' => $clean_params,
                ))
                ->where('product_id', '=', $this->_product->id)
                ->execute();
        
        //echo '<pre>';
        //echo $color.'<br />';
        //echo $consist.'<br />';
        //print_r($images);
        //print_r($params); 
    }
    
    protected function _get_clean_product_url()
    {
        // https://ad.admitad.com/goto/1d9ed345ddea57d2938cdc28f2033d/?ulp=http%3A%2F%2Fwww.wildberries.ru%2Fcatalog%2F1210559%2Fdetail.aspx
        // http%3A%2F%2Fwww.wildberries.ru%2Fcatalog%2F1210559%2Fdetail.aspx
        // http%3A%2F%2Fwww.wildberries.ru%2Fcatalog%2F1210559%2Fdetail.aspx
        
        $url = $this->_product->url;
        if (strstr($url, 'admitad.com') !== false) {
            $delimeter = '?ulp=';
            $pos = strpos($url, $delimeter) + strlen($delimeter);
            $url = substr($url, $pos);
            $url = str_replace(array('%3A', '%2F'), array(':', '/'), $url);
        }
        return $url;
    }
    
    protected function _encode($text)
    {
        return iconv('cp1251', 'utf-8', $text);
    }
    
    protected function _get_color($data)
    {
        return $this->_cut_str('class="color">', '</span>', $data);    
    }
    
    protected function _get_consist($data)
    {
        return $this->_cut_str('class="composition">', '</span>', $data);    
    }
        
    /*protected function _get_color_products($data)
    {
        $data = $this->_cut_str('<div id="colorpicker">', '</div>', $data);
        
        while ($color = $this->_cut_str('<li ', '</li>', $data)) {
                
        }    
    }  */
        
    protected function _get_images($data)
    { 
        $images = array();
              
        $data = $this->_cut_str('<ul class="carousel"', '</ul>', $data);
        while ($image = $this->_cut_str('<li>', '</li>', $data)) {
            $image = $this->_cut_str('rev="', '"', $image);
            if ($image) {
                $images[] = $image;   
            }        
        } 
        return $images; 
    }
        
    protected function _get_params($data)
    {
        $params = array();
              
        $data = $this->_cut_str('<table class="pp-additional">', '</table>', $data);
        while ($param = $this->_cut_str('<tr', '</tr>', $data)) {
            $name  = trim(str_replace(':', '', $this->_cut_str('<td>', '</td>', $param)));
            $value = trim($this->_cut_str('<td>', '</td>', $param));
            
            if ($name && $value) {
                $params[$name] = $value;   
            }        
        } 
        return $params;    
    }
}